<?php
/**
 * @author yjmp
 * @description : 商家注册页面
 */

define('IN_ECS', true);
define('ECS_ADMIN', true);

require(dirname(__FILE__) . '/includes/init.php');
require(dirname(__FILE__) . '/includes/cls_image.php');

$image = new cls_image($_CFG['bgcolor']);
$act = !empty($_REQUEST['act'])? $_REQUEST['act'] : 'reg';

/*用户数据*/
$user_info = $GLOBALS['db']->getRow('SELECT * FROM '.$GLOBALS['ecs']->table('users').' WHERE wecha_id = "'.$_SESSION['openids'].'"');
if ($user_info) {
    $_SESSION['user_id'] = $user_info['user_id'];
    $smarty->assign('user_info',$user_info);
}
/*注册页面显示*/
if ($act == 'reg') {
    //数据获取
    //返点
    $return_rate = $GLOBALS['db']->getOne('SELECT value FROM '.$GLOBALS['ecs']->table('shop_config').' WHERE code = "商家佣金率"');
    if (!empty($return_rate)) {
        $return_rate_format = explode(',',$return_rate); 
        $smarty->assign('return_rate',$return_rate_format);
    }
    $is_reg = $GLOBALS['db']->getOne(" SELECT COUNT(*) FROM ".$GLOBALS['ecs']->table('goods')." WHERE user_id = '$_SESSION[user_id]'");
    if ($is_reg) {
        $content = '请不要重复申请';
        send_err_msg($content);
    }else{
        $smarty->display('shop_reg.dwt');exit;
    }
    
}
/*注册事件完成*/
else if ($act == 'commit') {
    /*数据接收*/
    $shop_name = !empty($_POST['shopname'])? trim($_POST['shopname']) : '';
    $shop_addr = !empty($_POST['shopaddr'])? trim($_POST['shopaddr']) : '';
    $Lat = !empty($_POST['Lat'])? trim($_POST['Lat']) : '';
    $Lng = !empty($_POST['Lng'])? trim($_POST['Lng']) : '';
    $tel = !empty($_POST['tel'])? trim($_POST['tel']) : '';
    $phone = !empty($_POST['phone'])? trim($_POST['phone']) : '';
    $rebate = !empty($_POST['rebate'])? trim($_POST['rebate']) : '';
    $password = !empty($_POST['password'])? trim($_POST['password']) : '';
    $shop_desc = !empty($_POST['info'])? trim($_POST['info']) : '';

    /*数据验证*/
    if ($shop_name == '') {
        $content = '商店名不能为空';
        send_err_msg($content);
    }
    if ($shop_addr == '') {
        $content = '商店地址不能为空';
        send_err_msg($content);
    }
    if ($Lat == '') {
        $content = '请选择商店位置';
        send_err_msg($content);
    }
    if ($tel == '') {
        $content = '联系电话不能为空';
        send_err_msg($content);
    }
    if ($phone == '') {
        $content = '商家手机不能为空';
        send_err_msg($content);
    }
    if ($password == '') {
        $content = '密码不能为空';
        send_err_msg($content);
    }
    if ($shop_desc == '') {
        $content = '商家介绍不能为空';
        send_err_msg($content);
    }
    if (!is_reged($tel)) {
        $content = '该手机号码已被使用';
        send_err_msg($content);
    }
    /*数据处理*/
    $pwd_format = md5(md5($password).'10086'); // 密码加密
    $user_id = $_SESSION['user_id'];

     // 相册图片
    if (count($_FILES['img_url']['size']) > 1) {
        foreach ($_FILES['img_url']['tmp_name'] AS $key => $value)
        {
            if ($key < (count($_FILES['img_url']['tmp_name']) - 2))
            {
                if (!$image->check_img_type($_FILES['img_url']['type'][$key]))
                {
                    //sys_msg(sprintf($_LANG['invalid_img_url'], $key + 1), 1, array(), false);
                    $content = '图片格式不正确';
                    send_err_msg($content);
                }
            }
        }
        
        /* 处理商品图片 */
        $goods_img        = '';  // 初始化商品图片
        $goods_thumb      = '';  // 初始化商品缩略图
        $original_img     = '';  // 初始化原始图片
        $old_original_img = '';  // 初始化原始图片旧图
        $goods_galley = array();

        foreach ($_FILES['img_url']['tmp_name'] as $key => $value) {
           if ($_FILES['img_url']['tmp_name'] != '') {
               $img_obj = array();
               $img_obj['name'] = $_FILES['img_url']['name'][$key];
               $img_obj['type'] = $_FILES['img_url']['type'][$key];
               $img_obj['tmp_name'] = $_FILES['img_url']['tmp_name'][$key];
               $img_obj['error'] = $_FILES['img_url']['error'][$key];
               $img_obj['size'] = $_FILES['img_url']['size'][$key];

               $url = $image->upload_image($img_obj);
               if ($key == 0) {
                   $goods_thumb = $url;
               }else{
                $goods_galley[$key-1] = $url;
               }
           }
        }
    }

    /*数据录入*/
    $sql = ' INSERT INTO '.$GLOBALS['ecs']->table('goods').'( goods_name, goods_desc, address, latitude, longitude, tel, return_rate, pwd, is_on_sale, goods_number ,user_id, goods_thumb, goods_img, phone  )  VALUES ( "'.$shop_name.'", "'.$shop_desc.'", "'.$shop_addr.'", "'.$Lat.'", "'.$Lng.'", "'.$tel.'", "'.$rebate.'", "'.$pwd_format.'", 0, 1,"'.$user_id.'","'.$goods_thumb.'","'.$goods_thumb.'","'.$phone.'" ) ';
    if ($GLOBALS['db']->query($sql)) {
        $from_shop = $GLOBALS['db']->insert_id();
        $GLOBALS['db']->query(" UPDATE ".$GLOBALS['ecs']->table('users')." SET is_shoper = '1',from_shop = '$from_shop' WHERE user_id = '$user_id' ");

        /*加入相册图片*/
        if (count($goods_galley) > 0) {
            foreach ($goods_galley as $k => $v) {
                $GLOBALS['db']->query(" INSERT INTO ".$GLOBALS['ecs']->table('goods_gallery')." ( goods_id,img_url ) VALUES ('$from_shop','$v') ");
            }
        }

        $content = '申请已发出，请耐心等待审核';
        $type = 'success';
        $link = 'index.php';
        show_yj_message($content,$link,$type);
        exit;
    }
    $content = '添加失败';
    send_err_msg($content);
}

//消息类型测试
else if ($act == 'test') {
    //内容，链接，类型 success, error
    $content = '消息内容测试';
    $type = 'err';
    $link = 'javascript:history.back()';
    show_yj_message($content,$link,$type);
}
/**
 * is registed?
 * @param  string  $tel 商家手机号
 * @return boolean      
 */
function is_reged($tel)
{
    if ($tel != '') {
        $is_reged = $GLOBALS['db']->getOne('SELECT COUNT(*) FROM '.$GLOBALS['ecs']->table('goods').' WHERE tel = "'.$tel.'"');
        return $is_reged? false:true;
    }
    return false;
}
?>
